{% extends "menu.html" %}

{% block title %}术语广场{% endblock %}

{% block content %}
<div class="container mx-auto px-4 py-8">
    <h1 class="text-3xl font-bold mb-6">术语广场</h1>
    <div id="term-collections" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
        <!-- 术语集将在这里动态加载 -->
    </div>
</div>

<!-- 术语详情模态框 -->
<div id="term-modal" class="fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full hidden">
    <div class="relative top-20 mx-auto p-5 border w-11/12 md:w-3/4 lg:w-1/2 shadow-lg rounded-md bg-white">
        <div id="modal-content">
            <!-- 术语详情将在这里动态加载 -->
        </div>
        <div class="text-right mt-4">
            <button onclick="closeModal()" class="px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-700">关闭</button>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    fetchTermCollections();
});

async function fetchTermCollections() {
    try {
        const response = await fetch('/public_term_collections');
        const collections = await response.json();
        const container = document.getElementById('term-collections');
        container.innerHTML = collections.map(collection => `
            <div class="bg-white shadow-md rounded-lg p-6">
                <h2 class="text-xl font-semibold mb-2">${collection.table_name}</h2>
                <p class="text-gray-600 mb-2">作者: ${collection.user_nickname}</p>
                <p class="text-gray-600 mb-2">下载量: ${collection.download_count}</p>
                <p class="text-gray-700 mb-4">${collection.description || '无描述'}</p>
                <div class="flex justify-between">
                    <button onclick="viewDetails(${collection.id})" class="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">查看详情</button>
                    <button onclick="downloadCollection(${collection.id})" class="bg-green-500 text-white px-4 py-2 rounded hover:bg-green-600">下载</button>
                </div>
            </div>
        `).join('');
    } catch (error) {
        console.error('Error fetching term collections:', error);
    }
}

async function viewDetails(collectionId) {
    try {
        const response = await fetch(`/term_collection/${collectionId}`);
        const data = await response.json();
        const modalContent = document.getElementById('modal-content');
        modalContent.innerHTML = `
            <h2 class="text-2xl font-bold mb-4">${data.collection.table_name}</h2>
            <p class="mb-4">${data.collection.description || '无描述'}</p>
            <h3 class="text-xl font-semibold mb-2">术语列表:</h3>
            <ul class="list-disc pl-5">
                ${data.terms.map(term => `
                    <li class="mb-2">
                        <strong>${term.CN}</strong> - ${term.EN}
                        ${term.example ? `<br><em>例句: ${term.example}</em>` : ''}
                    </li>
                `).join('')}
            </ul>
        `;
        document.getElementById('term-modal').classList.remove('hidden');
    } catch (error) {
        console.error('Error fetching term collection details:', error);
    }
}

function closeModal() {
    document.getElementById('term-modal').classList.add('hidden');
}

async function downloadCollection(collectionId) {
    try {
        const response = await fetch(`/download_term_collection/${collectionId}`);
        const blob = await response.blob();
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.style.display = 'none';
        a.href = url;
        a.download = `term_collection_${collectionId}.csv`;
        document.body.appendChild(a);
        a.click();
        window.URL.revokeObjectURL(url);
    } catch (error) {
        console.error('Error downloading term collection:', error);
    }
}
</script>
{% endblock %}